package isv

import (
    "time"
	"dhfshop/models/isv"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/validation"
)

type PayParaController struct {
	base.BaseController
}

func (this *PayParaController) List() {
	query,_,fields,order_by,offset,limit,err := this.GetQueryPara()
	if err != nil {
	   this.ServeError(err.Error())
	   return
	}
	l, num, err := isv.GetAllAcPayParams(query,fields,order_by,offset,limit)
	if err != nil {
	    this.ServeError(err.Error())
	    return
	   
	} 
	r := struct {
	       TotalCount  int64               `json:"total_count"`
	       PayParams   []*isv.AcPayParams  `json:"pay_params"`
	    }{TotalCount: num, PayParams: l}
	this.Data["json"] = r
	this.ServeJSON()
}


func (this *PayParaController) Post() {
	var v isv.AcPayParams
	this.ParseForm(&v)
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	valid := validation.Validation{}
	valid.Required(v.PartnerId, "partner_id")
	valid.Required(v.CustAccountId, "cust_account_id")
	valid.Range(v.PayWayId, 1, 20, "pay_way_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
	if v.PayWayId == 1 && len(v.MediaId) < 3 {
	    this.ServeError("parameter media_id is invalid")
        return
	}
	if v.PayWayId == 2 {
	    if v.MchTypeId == 1 {
		    if v.AppId == "" || v.MchId == "" || v.Appkey == "" {
				this.ServeError("please confirm parameter app_id,mch_id,appkey is valid")
				return
			}
		}
		if v.MchTypeId == 2 {
		    if v.AppId == "" || v.MchId == "" || v.Appkey == "" || v.SubAppId == "" || v.SubMchId == "" {
				this.ServeError("please confirm parameter app_id,mch_id,appkey,sub_app_id,sub_mch_id is valid")
				return
			}
		}
	}
	var err error
	v.CustAccount, err = isv.GetCustAccountById(v.CustAccountId)
	if err != nil {
		this.ServeError(err.Error())
        return
	}
	v.CurAppId = v.CustAccount.AppId
	v.StatusId = 1
	_, err = isv.AddAcPayParams(&v)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Id        int64        `json:"pay_params_id"`
	    Created   time.Time    `json:"created"`
	}{Id: v.PayParamsId, Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *PayParaController) Put() {
    v := isv.AcPayParams{}
	this.ParseForm(&v)
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	valid := validation.Validation{}
	valid.Required(v.PartnerId, "partner_id")
	valid.Required(v.CustAccountId, "cust_account_id")
	//valid.Range(v.PayWayId, 1, 20, "pay_way_id")
	passed, _ := valid.Valid(&v)
	if !passed {
	    this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
	/*
	if v.PayWayId == 1 && len(v.MediaId) < 3 {
	    this.ServeError("parameter media_id is invalid")
        return
	}
	if v.PayWayId == 2 {
	    if v.MchTypeId == 1 {
		    if v.AppId == "" || v.MchId == "" || v.Appkey == "" {
				this.ServeError("please confirm parameter app_id,mch_id,appkey is valid")
				return
			}
		}
		if v.MchTypeId == 2 {
		    if v.AppId == "" || v.MchId == "" || v.Appkey == "" || v.SubAppId == "" || v.SubMchId == "" {
				this.ServeError("please confirm parameter app_id,mch_id,appkey,sub_app_id,sub_mch_id is valid")
				return
			}
		}
	}
	*/
	var err error
	if v.CustAccountId > 0 {
	    v.CustAccount, err = isv.GetCustAccountById(v.CustAccountId)
		if err != nil {
		    this.ServeError(err.Error())
		    return
		}
	}
	v.CurAppId = v.CustAccount.AppId
	excludeArr := []string{""}
	updateFields, err := this.GetUpdateFields(v, "", "form", excludeArr)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	updateFields = append(updateFields, "cur_app_id")
	if _, err := isv.UpdateAcPayParamsById(&v, updateFields...); err != nil{
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		    Id       int64       `json:"pay_params_id"`
	        Modified  time.Time   `json:"modified"`
	}{Id:v.PayParamsId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}	

// 
func (this *PayParaController) Delete() {
	paramsId, err := this.GetInt("pay_params_id")
	if err != nil {
	    this.ServeError(err.Error())
	    return
	}
	if err := isv.DeleteAcPayParamsById(int64(paramsId)); err != nil {
	    this.ServeError(err.Error())
	    return
	}
    r := struct {
		    Id        int         `json:"pay_params_id"`
	        Modified  time.Time   `json:"modified"`
	    }{Id:paramsId, Modified:time.Now().UTC()}
	this.Data["json"] = r 
	this.ServeJSON()
}



